#include "geminai_selfcheck.h"



#define LTE_CODE    60301
#define WIFI_CODE   60302
#define BT_CODE     60303
#define MPU_CODE    60304
#define CAMERA_CODE         61004   //陈列
#define VIDEODEVICE_CODE    61006   //动销
#define LIGHT_CODE          60311
#define LOGO_CODE           60310
#define COM_CODE            60312
#define POWER_CODE          60314
#define NTC_CODE            60315
#define COMPRS_CODE         60316
#define FAN_CODE            60317
#define BATTERY_CODE        60321
#define NTCENVIRONMENT_CODE 60340
#define ADAPTER_CODE        60341

check_flags check_result;

void setup_selfcheck_lte_flag(void)
{
    check_result.lte = 1;
}

void setdown_selfcheck_lte_flag(void)
{
    check_result.lte = 0;
}

//进行检测前先将adc,gpio,pwm,4g等初始化
void selfcheck_task(void *paramer)
{
    uint8_t i=0;

    BAT_EN_OFF;
    //风机 功率大于3
    RLY_FANS_ON;    
    while(1)    
    {
        Meter_Recv_once();
        if(runingInf.power < 3000){
            vTaskDelay(pdMS_TO_TICKS(1000));
        }
        else{
            check_result.fan = 1;
            break;
        }
        i++;
        if(i>20){
          break;
        }
    }
    GEMINAI_LOGI("selfcheck", "fans check vol= %ld mv , I = %d mA , power = %ld mW",runingInf.voltage,runingInf.electricity,runingInf.power);

    //压缩机 功率大于50
    RLY_COMP_ON;
    i=0;
    while(1)    
    {
        Meter_Recv_once();
        if(runingInf.power < 50000){
            vTaskDelay(pdMS_TO_TICKS(1000));
        }
        else{
            check_result.comprs = 1;
            break;
        }
        i++;
        if(i>20){
          break;
        }
    }
    GEMINAI_LOGI("selfcheck", "comprs check vol= %ld mv , I = %d mA , power = %ld mW",runingInf.voltage,runingInf.electricity,runingInf.power);
    //功率计 测量出电压220v(正负20%范围内)
    if(runingInf.voltage/1000 > 176 && runingInf.voltage/1000 < 264)
        check_result.powerMeter = 1;


    //灯光检测（大于20mA认为正常）
    if(adc_light_read() > 0.020)
    {
        check_result.light = 1;
    }

    //适配器监测
    if(_24V_INT_Read() > 20)
    {
        check_result.adapter = 1;
    }

    uint8_t j = 0;
    //柜内温探头检测
    while(1){
    if(ntc0_get_temp() > TEMP_ERR_LOWER && ntc0_get_temp() < TEMP_ERR_UPPER)
    {
        check_result.ntc0 = 1;
        break;
        }
        j++;
        if(j>3){
          break;
        }
        vTaskDelay(pdMS_TO_TICKS(1000));
    }

    //环境温探头检测
    j = 0;
    while (1)
    {
    if(get_NTCrx() > TEMP_ERR_LOWER && get_NTCrx() < TEMP_ERR_UPPER)
    {
        check_result.ntcTA = 1;
        }
        j++;
        if(j>3){
          break;
        }
        vTaskDelay(pdMS_TO_TICKS(1000));
    }

    //电测检测
    BAT_EN_OFF;
    if(read_battery_voltage() > 3 && read_battery_voltage() < 4.3)
    {
        check_result.battery = 1;
    }
    BAT_EN_ON;

    //wifi
    // int8_t para;
    // esp_err_t ret = esp_wifi_get_max_tx_power(&para);
    // if(ret == ESP_OK && para > 0)
    // {
        check_result.wifi = 1;
    // }

    //bt（没有用上，暂时不做检测）
    check_result.bt = 1;

    //4g
    i=0;
    while(check_result.lte == 0)
    {
        vTaskDelay(pdMS_TO_TICKS(3000));
        i++;
        if(i>3){
          break;
        }
    }
    GEMINAI_LOGI("selfcheck", "fan:%d,comprs:%d,powerMeter:%d,light:%d,battery:%d,wifi:%d,bt:%d,lte:%d,adapter:%d,ntc0:%d,ntcTA:%d"\
                ,check_result.fan,check_result.comprs,check_result.powerMeter,check_result.light,check_result.battery,\
                check_result.wifi,check_result.bt,check_result.lte,check_result.adapter,check_result.ntc0,check_result.ntcTA);

    int count = 0;

    if(check_result.fan == 0){count++;}
    if(check_result.comprs == 0){count++;}
    if(check_result.powerMeter == 0){count++;}
    if(check_result.light == 0){count++;}
    if(check_result.battery == 0){count++;}
    if(check_result.wifi == 0){count++;}
    if(check_result.bt == 0){count++;}
    if(check_result.lte == 0){count++;}
    if(check_result.adapter == 0){count++;}
    if(check_result.ntc0 == 0){count++;}
    if(check_result.ntcTA == 0){count++;}

    if(count != 0){
        count = 300/count;

        if(check_result.fan == 0){app_play_wav(count, FAN_ERROR_AUDIO);}
        if(check_result.comprs == 0){app_play_wav(count, COMP_ERROR_AUDIO);}
        if(check_result.powerMeter == 0){app_play_wav(count, POWER_METER_ERROR_AUDIO);}
        if(check_result.light == 0){app_play_wav(count, LOGO_LAMP_ERROR_AUDIO); app_play_wav(count, ATMOSPHERE_LAMP_ERROR_AUDIO);}
        if(check_result.battery == 0){app_play_wav(count, BATTERY_ERROR_AUDIO);}
        if(check_result.wifi == 0){app_play_wav(count, WIFI_ERROR_AUDIO);}
        if(check_result.bt == 0){app_play_wav(count, BT_ERROR_AUDIO);}
        if(check_result.lte == 0){app_play_wav(count, LTE_ERROR_AUDIO);}
        // if(check_result.adapter == 0){app_play_wav(count, ADAPTER_ERROR_AUDIO);}
        if(check_result.ntc0 == 0){app_play_wav(count, NTC_ERROR_AUDIO);}
        if(check_result.ntcTA == 0){app_play_wav(count, ENVIRONMENT_NTC_ERROR_AUDIO);}
    }else{
        app_play_wav(1, ERR_CPU_TEST_OK_AUDIO);
    }




    send_selfcheck_result();

}

void fill_selfcheck_result(cJSON *result, int code, int ret)
{
    cJSON *item = cJSON_CreateObject();
    if(ret){
        cJSON_AddBoolToObject(item, "is_occursed", false);
        cJSON_AddStringToObject(item, "msg", "ok");
    }
    else{
        cJSON_AddStringToObject(item, "msg", "fail");
        cJSON_AddBoolToObject(item, "is_occursed", true);
    }
    cJSON_AddNumberToObject(item, "check_duration", 0);
    cJSON_AddNumberToObject(item, "check_num", 1);
    cJSON_AddNumberToObject(item, "part_code", code);
    cJSON_AddItemToArray(result, item);
}

void send_selfcheck_result(void)
{
    char *json_string = NULL;    
    cJSON *root = cJSON_CreateObject();
    if (root == NULL) {
        printf("Failed to create JSON object\n");
        return ;
    }
    time_t now;    
    // 获取当前系统时间
    time(&now);
    char buffer[24]={0}; // 假设足够存储转换后的字符串

    // 使用 snprintf 将整数转换为字符串
    snprintf(buffer, sizeof(buffer), "%lld", now*1000);

    cJSON *result = cJSON_CreateArray();

    //柜内温度传感器
    fill_selfcheck_result(result, NTC_CODE, check_result.ntc0);    
    //环境温度传感器
    fill_selfcheck_result(result, NTCENVIRONMENT_CODE, check_result.ntcTA);  
    //功率计
    fill_selfcheck_result(result, POWER_CODE, check_result.powerMeter); 
    //压缩机
    fill_selfcheck_result(result, COMPRS_CODE, check_result.comprs);
    //风机
    fill_selfcheck_result(result, FAN_CODE, check_result.fan);
    //灯
    fill_selfcheck_result(result, LIGHT_CODE, check_result.light);    
    //电池
    fill_selfcheck_result(result, BATTERY_CODE, check_result.battery);
    //wifi
    fill_selfcheck_result(result, WIFI_CODE, check_result.wifi);
    //bt
    fill_selfcheck_result(result, BT_CODE, check_result.bt);
    //lte
    fill_selfcheck_result(result, LTE_CODE, check_result.lte);
    //适配器
    fill_selfcheck_result(result, ADAPTER_CODE, check_result.adapter);
    

    cJSON_AddItemToObject(root, "result", result);
    cJSON_AddStringToObject(root, "asset_id", base_station.asset_id);
    cJSON_AddStringToObject(root, "event_time", buffer);
    cJSON_AddNumberToObject(root, "check_type", 1);


    json_string = cJSON_PrintUnformatted (root);
    aliyun_send_event_mqtt(AIOT_IAT_EVENT_CHECK_EVENT, json_string);
    printf("self:check =[%s]\n", json_string);
    cJSON_Delete(root);

}

/*
    app_play_wav("/spiffs/battery_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/logo_lamp_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/bluetooth_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/comp_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/display_camera_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/environment_ntc_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/fan_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/gyro_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/lte_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/ntc_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/power_meter_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/sale_camera_error.wav");
    vTaskDelay(3000 / portTICK_PERIOD_MS);
    app_play_wav("/spiffs/wifi_error.wav");
 */